* code that identifies mothers around the DACA age cutpoint and gets claims for all their matched kids
global path = "/Users/jhainmueller/Box Sync/JHMedBox/"
clear all
set more off 

* load claims of women (aged 12-51)
cd "$path/cawem_wkdata/data/working"
use women.dta, clear

* merge in matched infant IDs
gen m_SAK_RECIP = SAK_R 
capt drop rowobs 
gen rowobs = _n
sort m_SAK_RECIP pregno rowobs, stable
merge m:1 m_SAK_RECIP pregno using "$path/cawem_wkdata/data/pregkidmerge/crosswalkpregtokid.dta" , sorted keep(3)
keep if _merge==3

* sample restrictions

* reduce to one row per pregnancy and live birth
gen  livebirth1  = inlist(pregtype,"LB","LBANDSB","LBORSB","PTB")
egen livebirth   = max(livebirth1) , by(SAK_RECIP pregno)
drop livebirth1
keep if livebirth==1
keep if pregdatemark

* only matches within 0-2 days 
* between date of delivery and infant date of birth
keep if match=="HHID and day 0"             | match=="HHID and day 1"             | match=="HHID and day 2" | ///
        match=="Zips, Eth, Race, and day 0" | match=="Zips, Eth, Race, and day 1" | match=="Zips, Eth, Race, and day 2"

* month of claim
gen month= month(daten)

* DACA eligible if mother was born after 6/15/81
* mother date of birth
gen    dob = date(DTE_BIRTH,"YMD")
format dob %td

gen yob = year(dob)
gen mob = month(dob)

* only keep mothers born between 1980 and 1982 
keep if yob>=1980 & yob<=1982

* add pre-fix to seperate mother vars from kid vars
renvars yob dob mob DTE_BIRTH CX CWCX SAK_R match pregno SAK_CASE , prefix(mother_)
keep mother_* k_SAK_RECIP

capt drop rowobs
gen rowobs = _n
sort k_SAK_RECIP rowobs, stable

* store data to pull from
save "$path/daca_oregon_notshared/data/kidsformerge.dta", replace


/// now pull claims for all matched kids

* set up empty data
cd  "$path/cawem_wkdata/data/merged"
use  mergedQ12003_OHSU_CLMS if year==9, clear
cd  "$path/daca_oregon_notshared/data/"
save allkids2.dta, replace

* loop over all years and quarters and keep claims of matched kids
 foreach y of numlist 2003(1)2015 {
 foreach q of numlist 1(1)4 {
 
   timer on 1
   
   di ""
   di ""
   di "---------------------"
   di "year `y' quarter `q' " 
   cd "$path/cawem_wkdata/data/merged"

   use "mergedQ`q'`y'_OHSU_CLMS", clear
   keep if mergedclmsrecips==1
   gen       k_SAK_RECIP = SAK_RECIP
   sort k_SAK_RECIP, stable
   merge m:1 k_SAK_RECIP using "$path/daca_oregon_notshared/data/kidsformerge.dta" , keep(3) sorted
   keep if _merge==3

   cd  "$path/daca_oregon_notshared/data/"
  
append using allkids2 
save   allkids2 , replace
   
timer off 1
timer list
  
  }
}

cd  "$path/daca_oregon_notshared/data/"
use allkids2.dta, clear

* pre-process ICD 9 diagnoses codes
gen  CDE_DIAG_1 = CDE_DIAG_PRIM
drop CDE_DIAG_PRIM

forvalues i = 1/4 {

gen      code2_`i'        = substr(CDE_DIAG_`i',1,2)
destring code2_`i' , force  gen(code2_`i'n)

gen      code3_`i'        = substr(CDE_DIAG_`i',1,3)
destring code3_`i' , force  gen(code3_`i'n)

gen      code4_`i'        = substr(CDE_DIAG_`i',1,4)
destring code4_`i' , force  gen(code4_`i'n)

}

* save kids claims data with mother info
save   allkids2 , replace
